package com.gears_software.sxdc.dccx.ui.activity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.adapter.RecomandAddressAdapter;
import com.gears_software.sxdc.dccx.data.model.PositionEntity;
import com.gears_software.sxdc.dccx.data.sharedPreference.DCCXSp_;
import com.gears_software.sxdc.dccx.ui.map.PoiSearchTask;
import com.gears_software.sxdc.dccx.ui.map.RouteTask;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.sharedpreferences.Pref;

import java.util.ArrayList;
import java.util.List;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by chezhenzhen on 2017/9/1.
 */

@EActivity(R.layout.activity_destination)
public class DestinationActivity extends AppCompatActivity {

    @App
    DCCXApplication app;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView right_icon;
    @ViewById
    ListView list_address;
    @ViewById
    EditText edit_destination;
    @ViewById
    TextView txt_search;
    @ViewById
    TextView txt_current_city;
    @Extra
    String title;
    @Extra
    String flag;
    @Extra
    String currentCity;
    private RouteTask mRouteTask;
    RecomandAddressAdapter adapter;
    List<PositionEntity> list = new ArrayList<PositionEntity>();

    @AfterViews
    void created() {
        initIconFont();
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));

        list.add(new PositionEntity(37.846744, 112.579526, "玲珑琚", "迎泽区双塔东街"));
        list.add(new PositionEntity(37.869931,112.569963,"宝地小区","迎泽区柳巷72号"));
        list.add(new PositionEntity(37.896932,112.594615,"富力城A区","杏花岭区34号"));
        list.add(new PositionEntity(37.790604, 112.610566, "太原南站", "小店区太榆路"));
        list.add(new PositionEntity(37.884996, 112.586521, "太原东站", "杏花岭区建设北路534号"));
        list.add(new PositionEntity(37.873592, 112.52137, "华宇购物广场(漪汾街)", "万柏林区漪汾街88号"));
        list.add(new PositionEntity(37.823728, 112.563359, "百盛(亲贤北街店)", "小店区亲贤北街189号"));
        list.add(new PositionEntity(37.869361, 112.577223, "铜锣湾国际购物中心", "迎泽区五一路77号"));
        list.add(new PositionEntity(37.875165, 112.596577, "东湖醋园", "杏花岭马道坡26号"));
        list.add(new PositionEntity(37.780781, 112.446942, "蒙山大佛旅游区", "晋源区寺底村"));
        list.add(new PositionEntity(37.865077, 112.550676, "天美名店", "迎泽区新建路39号1-6层"));

        if (currentCity != null && !currentCity.equals("")) {
            txt_current_city.setText(currentCity);
        }
        adapter = new RecomandAddressAdapter(this, list);
        list_address.setAdapter(adapter);
        mRouteTask = RouteTask.getInstance(getApplicationContext());
        edit_destination.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                InputTipTask.getInstance(adapter).searchTips(getApplicationContext(),s.toString(),RouteTask.getInstance(getApplicationContext()).getStartPoint().city);
                if (RouteTask.getInstance(getApplicationContext()).getStartPoint() == null) {
                    Toast.makeText(getApplicationContext(), "检查网络，Key等问题", Toast.LENGTH_SHORT).show();
                    return;
                }
                PoiSearchTask poiSearchTask = new PoiSearchTask(getApplicationContext(), adapter);
                poiSearchTask.search(s.toString(), RouteTask.getInstance(getApplicationContext()).getStartPoint().getCity());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    void initIconFont() {
        actionbar_title.setText(title);
        back_icon.setVisibility(View.VISIBLE);
        back_icon.setText(R.string.back_icon);
        back_icon.setTypeface(app.iconfont);
        right_icon.setVisibility(View.GONE);
        txt_search.setTypeface(app.iconfont);
    }


    @ItemClick
    void list_address(int position) {
        PositionEntity entity = (PositionEntity) adapter.getItem(position);
        if (flag != null && !flag.equals("")) {
            if (flag.equals("end")) {
                mRouteTask.setEndPoint(entity);
                mRouteTask.search();
                Intent intent = new Intent();
                intent.putExtra("entity", entity);
                setResult(2, intent);
            } else if (flag.equals("start")) {
                mRouteTask.setStartPoint(entity);
                mRouteTask.search();
                Intent intent = new Intent();
                intent.putExtra("entity", entity);
                setResult(1, intent);
            }
        }

        this.finish();
    }

    @Click
    void back_icon() {
        this.finish();
    }

}
